package com.cheng.xbjy.service.impl;

import com.cheng.xbjy.dao.DeptDao;
import com.cheng.xbjy.dao.UserDao;
import com.cheng.xbjy.entity.Dept;
import com.cheng.xbjy.entity.User;
import com.cheng.xbjy.entity.UserFocus;
import com.cheng.xbjy.entity.vo.PageResult;
import com.cheng.xbjy.entity.vo.Result;
import com.cheng.xbjy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private DeptDao deptDao;


    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public void save(User user) {
        //设置默认属性
        user.setRegisterTime(new Date());   //注册时间
        user.setIsSecret("0");              //默认不私秘
        user.setLook(0L);                   //查看数默认为0

        //设置默认头像
        if (StringUtils.isEmpty(user.getPic())) {
            user.setPic("https://www.baidu.com/favicon.ico");
        }
        userDao.save(user);
    }

    @Transactional
    @Override
    public void updatePassword(User user) {
        userDao.updatePassword(user.getEmail(), user.getPassword());
    }

    @Override
    public User findOne(User user) {
        Optional<User> userOptional = userDao.findOne(Example.of(user));
        return userOptional.orElse(null);
    }

    @Transactional
    @Override
    public void updateLoginTime(Long id) {
        userDao.updateLoginTime(id, new Date());
    }

    @Override
    public User findById(Long userId) {
        return userDao.findById(userId).get();
    }

    @Override
    public Page<User> findPageUser(String username, Integer page) {
        if (StringUtils.isEmpty(username)) {
            return userDao.findAll(PageRequest.of(page - 1, PageResult.PAGE_SIZE));
        }
        return userDao.findByUsernameLike("%" + username + "%", PageRequest.of(page - 1, PageResult.PAGE_SIZE));
    }

    @Override
    public List<Long> findFocus(Long userId) {
        return userDao.findFocusByUserId(userId);
    }

    @Override
    public Integer countFocusByUserId(Long id) {
        return userDao.countFocusByUserId(id);
    }

    @Transactional
    @Override
    public void updateLookById(Long id) {
        userDao.updateLookById(id);
    }

    @Transactional
    @Override
    public Result addFocus(Long userId, Long focusId) {
        UserFocus userFocus = userDao.findByUserIdAndFocusId(userId, focusId);
        if (userFocus != null) {
            userDao.deleteFocus(userId, focusId);
            return new Result(true, "取消关注成功");
        }
        userDao.addFocus(userId, focusId);
        return new Result(true, "关注成功");
    }

    @Override
    public User update(User user) {
        User dbUser = userDao.findById(user.getId()).get();

        if (user.getDeptId() != null) {
            Dept dept = deptDao.findById(user.getDeptId()).get();
            dbUser.setDeptId(dept.getId());
            dbUser.setDeptName(dept.getName());
        }

        dbUser.setRealName(user.getRealName());
        dbUser.setPhone(user.getPhone());
        dbUser.setAge(user.getAge());
        dbUser.setGender(user.getGender());
        dbUser.setIsSecret(user.getIsSecret());

        userDao.save(dbUser);

        return dbUser;
    }

    @Transactional
    @Override
    public void updateUserPic(Long userId, String url) {
        userDao.updateUserPic(userId, url);
    }

    @Override
    public Page<User> findFocusPage(Long userId, Integer page) {
        return userDao.findFocusPage(userId, PageRequest.of(page - 1, PageResult.PAGE_SIZE));
    }

    @Override
    public List<User> findDeptUser(Long deptId) {
        return userDao.findDeptUser(deptId);
    }

    @Override
    public User findByWxOpenid(String wxOpenid) {
        return userDao.findByWxOpenid(wxOpenid);
    }
}
